package com.task.util;

import com.task.collector.ClockCollector;

import java.util.*;

public class LogCacheUtil {

    private Class aClass;
    private Integer cacheNum;


    public LogCacheUtil(Class clazz,Integer cacheNum){
        this.aClass=clazz;
        logArray=new ArrayList<>(cacheNum);
        loggerMap.put(clazz.getName(),logArray);
        this.cacheNum=cacheNum;
    }

    private static Map<String,List<String>> loggerMap=new HashMap<>(100);


    public static  List<String> getLogArray(String className) {
        return loggerMap.get(className);
    }

    private List<String> logArray;



    public void logger(String str) {

        StringBuffer buffer=new StringBuffer(aClass.getName());
        buffer.append(", ");
        buffer.append(new Date());
        buffer.append(":");
        buffer.append(str);

        if(loggerMap.get(aClass.getName()).size()<cacheNum) {
            LogCacheUtil.loggerMap.get(aClass.getName()).add(buffer.toString());
        }else{
            LogCacheUtil.loggerMap.get(aClass.getName()).remove(0);
            LogCacheUtil.loggerMap.get(aClass.getName()).add(buffer.toString());
        }
    }
}
